<?php

namespace App\Models;

use Core\Result\Result;
use Core\Sign\Sign;
use Core\Utils\Ip\Ipinit;

class UsersModel
{
    /**
     * 通过uuid获取用户id
     * entry conn,uuid
     * return id
     * */
    public static function getUserIdOnUuid($conn,$uuid){
        $sql = "select id from `users` where `id` = '$uuid'";
        return $conn->readConn($sql)["id"];
    }
    /**
     * 获取用户信息
     * entry conn,token
     * return array
     * */
    public static function getUserInside($conn,$token){
        $queryMemberSql="SELECT `id`,`token`,`username`, `truename`,`svip`,`phone`,`head`, `pid`,`level`,`level_time`, `group`, `money`, `myr`, `usdt`,`capping`, `id_card`, `login_ip`, `create_time`, `last_time`,`status`, `ach`, `one_ach`, `sach`, `share_rewards`, `push_reward`,`peer_reward`, `agent_reward`,`total_reward`,`collect`,`is_white`,`is_trade`,`is_get`,`caigou` FROM `users` WHERE  `token` = '$token'";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 获取用户信息(jiangjin)
     * entry conn,token
     * return array
     * */
    public static function getUserRewardsInside($conn,$token){
        $queryMemberSql="SELECT `id`, `capping`,`ach`, `one_ach`, `sach`, `share_rewards`, `push_reward`,`peer_reward`, `agent_reward`,`collect`,`caigou` FROM `users` WHERE  `token` = '$token'";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 获取用户代理申请
     * entry conn,token
     * return array
     * */
    public static function getUserPasswordInside($conn,$token){
        $queryMemberSql="SELECT `id`,`token`,`password`, `pay_password`,`username`,`phone` FROM `users` WHERE  `token` = '$token'";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 获取用户信息
     * entry conn,id
     * return array
     * */
    public static function getUserInsideId($conn,$id){
        $queryMemberSql="SELECT `id`,`token`,`username`, `truename`,`svip`,`phone`,`head`, `pid`,`level`,`level_time`, `group`, `money`, `myr`, `usdt`, `capping`,`id_card`, `login_ip`, `create_time`, `last_time`,`status`, `ach`, `one_ach`, `sach`, `share_rewards`, `push_reward`,`peer_reward`, `agent_reward`,`total_reward`,`is_white`,`is_trade` FROM `users` WHERE  `id` = '$id'";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 获取用户姓名
     * entry conn,id
     * return array
     * */
    public static function getUserNameInsideId($conn,$id){
        $queryMemberSql="SELECT `id`, `username`, `truename`, `phone`,`head` FROM `users` WHERE  `id` = '$id'";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 获取用户信息(不对外)
     * entry conn,username
     * return array
     * */
    public static function getUserInsideUsername($conn,$username){
        $queryMemberSql="SELECT `id`,`token`,`password`,`status`,`username`,`truename`,`money`,`usdt`,`myr`,`phone` FROM `users` WHERE  `username` = '$username'";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 获取手机号注册数量(不对外)
     * entry conn,phone
     * return array
     * */
    public static function getUserInsidePhoneCount($conn,$phone){
        $queryMemberSql="SELECT count(`id`) as ids FROM `users` WHERE  `phone` = '$phone'";
        return $conn->readConn($queryMemberSql)["ids"];
    }
    /**
     * 获取用户旗下大区id
     * entry conn,username
     * return array
     * */
    public static function getUserInsideBig($conn,$id){
        $queryMemberSql="select `id` from `users` where `pid` = $id order by `sach` desc,`create_time` asc limit 1";
        return $conn->readConn($queryMemberSql)["id"];
    }
    /**
     * 查询旗下所有直推业绩
     * entry UserExample->id
     * return array
     * */
    public static function getUserAllAchPid($conn,$id){
        $queryMemberSql="SELECT	sum(`ach`) as allAch FROM `users` WHERE `pid` = $id";
        return $conn->readConn($queryMemberSql)["allAch"];
    }
    /**
     * 查询旗下所有直推总业绩
     * entry UserExample->id
     * return array
     * */
    public static function getUserAllSachPid($conn,$id){
        $queryMemberSql="SELECT	sum(`ach`)  + sum(`sach`) as allAch FROM `users` WHERE `pid` = $id";
        return $conn->readConn($queryMemberSql)["allAch"];
    }
    /**
     * 获取用户旗下小区业绩
     * entry conn,username
     * return array
     * */
    public static function getUserInsideSmallAch($conn,$id){
        $queryMemberSql="select SUM(`ach`+ `sach`) - MAX(`ach` + `sach`) as Ach from `users` where `pid` = $id ";
        return $conn->readConn($queryMemberSql)["Ach"];
    }
    /**
     * 获取用户旗下小区业绩达到300万数量
     * entry conn,username
     * return array
     * */
    public static function getUserInsideSmallAch300($conn,$id){
        $queryMemberSql="select count(`id`) as count from `users` where `pid` = $id and `sach`+`ach` >=3000000";
        return $conn->readConn($queryMemberSql)["count"];
    }
    public static function getUserInsideSmallAchCount($conn,$id,$sach){
        $queryMemberSql="select count(`id`) as count from `users` where `pid` = $id and `sach`+`ach` >= $sach";
        return $conn->readConn($queryMemberSql)["count"];
    }
    /**
     * 插入用户信息
     * entry conn,token
     * return int
     * */
    public static function insertUserInside($conn,$UserExample){
        $insertSql = "insert into `users`(`token`,`username`,`password`,`phone`,`pid`,`create_time`,`pay_password`)value('$UserExample->token','$UserExample->username','$UserExample->password','$UserExample->phone',$UserExample->pid,$UserExample->create_time,'$UserExample->pay_password')";
        return $conn->executeConn($insertSql);
    }
    /**
     * 查询所有上级
     * entry conn,uid
     * return int
     * */
    public static function getUserSuperiorAllIds($conn,$id){
        $queryMemberSql="SELECT GROUP_CONCAT(`id`) as `ids` FROM (SELECT @r AS _id,(SELECT @r := pid FROM users WHERE id = _id) AS parent_id,@l := @l + 1 AS lvl FROM (SELECT @r := $id, @l := 0) vars, users h WHERE @r <> 0) T1 JOIN users T2 ON T1._id = T2.id where 1=1 ORDER BY lvl ASC";
        return $conn->readConn($queryMemberSql)["ids"];
    }
    /**
     * 登录成功
     * entry conn,token
     * return json
     * */
    public static function loginSuccess($conn,$UserExample)
    {
        $UserExample->login_ip =new Ipinit();
        $UserExample->last_time = "NOW()";
        self::upUserInside($conn, $UserExample);
        $sign = new Sign();
        Result::success($sign->encryptionToken($UserExample->token));
    }
    /**
     * 获取用户直推列表(经销商)
     * entry UserExample->id
     * return array
     * */
    public static function getUserTeamInside($conn,$UserExample){
        $queryMemberSql="SELECT `id`,`username`,`truename`,`capping`,`ach`,`sach`,`level`,`usdt`,`myr`,`head`,`create_time`,`svip` FROM `users` WHERE  `pid` = $UserExample->id and `ach` > 0 order by `id` desc";
        return $conn->readConn($queryMemberSql, true);
    }
    /**
     * 获取用户直推列表(注册会员)
     * entry UserExample->id
     * return array
     * */
    public static function getUserTeameNrollInside($conn,$UserExample){
        $queryMemberSql="SELECT `id`,`username`,`truename`,`capping`,`ach`,`sach`,`level`,`usdt`,`myr`,`head`,`create_time`,`svip` FROM `users` WHERE  `pid` = $UserExample->id and `ach` <= 0 order by `id` desc";
        return $conn->readConn($queryMemberSql, true);
    }
    /**
     * 获取用户所有上级
     * entry id
     * return array
     * */
    public static function getUserSuperiorAllPid($conn,$id){
        $queryMemberSql="SELECT `id`,`token`,`level`,`svip`,`ach`,`one_ach`,`sach`,`capping`,`peer_reward`,`money`,`agent_reward` FROM (SELECT @r AS _id,(SELECT @r := pid FROM users WHERE id = _id) AS parent_id,@l := @l + 1 AS lvl FROM (SELECT @r := $id, @l := 0) vars, users h WHERE @r <> 0) T1 JOIN users T2 ON T1._id = T2.id where 1=1 ORDER BY lvl ASC";
        return $conn->readConn($queryMemberSql,true);
    }
    /**
     * 获取用户所有上级sid
     * entry id
     * return array
     * */
    public static function getUserSuperiorAllSid($conn,$id){
        $queryMemberSql="SELECT `id`,`username`,`truename`,`capping`,`sach`,`ach`,`one_ach`,`sach`,`money`,`pid`,`peer_reward`,`share_rewards` FROM (SELECT @r AS _id,(SELECT @r := pid FROM users WHERE id = _id) AS parent_id,@l := @l + 1 AS lvl FROM (SELECT @r := $id, @l := 0) vars, users h WHERE @r <> 0) T1 JOIN users T2 ON T1._id = T2.id where 1=1 ORDER BY lvl ASC";
        return $conn->readConn($queryMemberSql,true);
    }
    /**
     * 增加用户业绩
     * entry UserExample
     * return int
     * */
    public static function editUserAchAddInside($conn,$ach,$oneMoney,$orderNumber,$UserExample){
        $upUserSql="UPDATE  `users` SET `ach` = `ach`+ $ach,`one_ach` = $oneMoney,`svip` = `svip` + $orderNumber WHERE `token` = '$UserExample->token' and `ach` = $UserExample->ach";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 增加用户业绩
     * entry UserExample
     * return int
     * */
    public static function editUserSachAddInside($conn,$ach,$UserExample){
        $upUserSql="UPDATE  `users` SET `sach` = `sach` + $ach WHERE `token` = '$UserExample->token' and `sach` = $UserExample->sach";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 增加用户动态奖励
     * entry UserExample
     * return int
     * */
    public static function editUserPeerRewardInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `peer_reward` = `peer_reward` + $balance WHERE `token` = '$UserExample->token' and `peer_reward` = $UserExample->peer_reward";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 减去用户余额
     * entry UserExample
     * return int
     * */
    public static function editUserMoneyOutInside($conn,$balance,$payMyr,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money`-$balance,`myr` = `myr` - $payMyr WHERE `token` = '$UserExample->token' and `money` = $UserExample->money and `myr` = $UserExample->myr";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 减去用户消费积分
     * entry UserExample
     * return int
     * */
    public static function editUserMyrOutInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `myr` = `myr`-$balance WHERE `token` = '$UserExample->token' and `myr` = $UserExample->myr";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 减去用户灵龙积分
     * entry UserExample
     * return int
     * */
    public static function editUserUsdtOutInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `usdt` = `usdt`-$balance WHERE `token` = '$UserExample->token' and `usdt` = $UserExample->usdt";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 增加用户消费积分
     * entry UserExample
     * return int
     * */
    public static function editUserMoneyAddInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money`+$balance WHERE `token` = '$UserExample->token' and `money` = $UserExample->money";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 增加用户交易积分
     * entry UserExample
     * return int
     * */
    public static function editUserMyrAddInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `myr` = `myr`+$balance WHERE `token` = '$UserExample->token' and `myr` = $UserExample->myr";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 增加用户灵龙积分
     * entry UserExample
     * return int
     * */
    public static function editUserUsdtAddInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `usdt` = `usdt`+$balance WHERE `token` = '$UserExample->token' and `usdt` = $UserExample->usdt";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 购买商品更新
     * entry UserExample
     * return int
     * */
    public static function editUserMoneyAndCappingInside($conn,$balance,$sch,$svip,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money`-$balance,`svip` = $svip,`ach` = `ach` + $sch,`one_ach` = $sch WHERE `token` = '$UserExample->token' and `money` = $UserExample->money and `ach` = $UserExample->ach and `one_ach` = $UserExample->one_ach";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 购买商品更新
     * entry UserExample
     * return int
     * */
    public static function editUserUsdtAndCappingInside($conn,$balance,$sch,$svip,$capping,$UserExample){
        $upUserSql="UPDATE  `users` SET `usdt` = `usdt`-$balance,`svip` = $svip,`ach` = `ach` + $sch,`one_ach` = $sch,`capping` = `capping` + $capping WHERE `token` = '$UserExample->token' and `usdt` = $UserExample->usdt and `ach` = $UserExample->ach and `one_ach` = $UserExample->one_ach and `capping` = $UserExample->capping";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 采购商品更新
     * entry UserExample
     * return int
     * */
    public static function editUserMoneyAndCaiGouInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money`-$balance,`caigou` = `caigou` + $balance WHERE `token` = '$UserExample->token' and `money` = $UserExample->money and `capping` = $UserExample->capping and `ach` = $UserExample->ach and `one_ach` = $UserExample->one_ach";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 兑换商品更新
     * entry UserExample
     * return int
     * */
    public static function editUserMoneyAndExchangeInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `usdt` = `usdt`-$balance WHERE `token` = '$UserExample->token' and `usdt` = $UserExample->usdt";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 分享积分领取
     * entry UserExample
     * return int
     * */
    public static function editUserWinningInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money` + $balance,`share_rewards` = `share_rewards` + $balance,`total_reward` = `total_reward` + $balance,`caigou` = `caigou` + 1 WHERE `token` = '$UserExample->token' and `money` = $UserExample->money";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 积分奖励领取
     * entry UserExample
     * return int
     * */
    public static function editUserSalesInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money` + $balance,`push_reward` = `push_reward` + $balance,`total_reward` = `total_reward` + $balance,`collect` = `collect` + 1 WHERE `token` = '$UserExample->token' and `money` = $UserExample->money";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 拓展积分领取
     * entry UserExample
     * return int
     * */
    public static function editUserBonusInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money` + $balance,`agent_reward` = 0,`peer_reward` = `peer_reward` + $balance,`total_reward` = `total_reward` + $balance WHERE `token` = '$UserExample->token' and `money` = $UserExample->money and `agent_reward` = $UserExample->agent_reward";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 拓展积分增加
     * entry UserExample
     * return int
     * */
    public static function editUserAgentInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `agent_reward` = `agent_reward` + $balance WHERE `token` = '$UserExample->token' and `money` = $UserExample->money and `agent_reward` = $UserExample->agent_reward";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 采购领取
     * entry UserExample
     * return int
     * */
    public static function editUserProcureInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money` + $balance,`total_reward` = `total_reward` + $balance,`caigou` = 0 WHERE `token` = '$UserExample->token' and `money` = $UserExample->money and `caigou` = $UserExample->caigou";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 获取用户旗下所有业绩
     * entry UserExample->id
     * return array
     * */
    public static function getUserAllSumAch($conn,$id){
        $queryMemberSql="SELECT	sum(`ach`) as ach FROM (SELECT @ids AS _ids,( SELECT @ids := GROUP_CONCAT( id ) FROM `users` WHERE FIND_IN_SET( pid, @ids ) ) AS cids,@l := @l + 1 AS layer FROM `users`,( SELECT @ids := $id, @l := 0 ) b WHERE @ids IS NOT NULL) ID, `users` DATA WHERE FIND_IN_SET( DATA.id, ID._ids )";
        return $conn->readConn($queryMemberSql)["ach"];
    }
    /**
     * 获取用户旗下所有会员
     * entry UserExample->id
     * return array
     * */
    public static function getUserAllMember($conn,$id){
        $queryMemberSql="SELECT	`id`,`username`,`pid`,`capping`,`sach` FROM (SELECT @ids AS _ids,( SELECT @ids := GROUP_CONCAT( id ) FROM `users` WHERE FIND_IN_SET( pid, @ids ) ) AS cids,@l := @l + 1 AS layer FROM `users`,( SELECT @ids := $id, @l := 0 ) b WHERE @ids IS NOT NULL) ID, `users` DATA WHERE FIND_IN_SET( DATA.id, ID._ids )";
        return $conn->readConn($queryMemberSql,true);
    }
    /**
     * 递归查询下级用户
     * entry UserExample->id
     * return array
     * */
    public static function getUserAllPid($conn,$id){
        $queryMemberSql="SELECT	`id`,`username`,`pid`,`capping`,`sach`,`peer_reward`,`share_rewards` FROM `users` WHERE `pid` in($id)";
        return $conn->readConn($queryMemberSql,true);
    }
    /**
     * 升级
     * entry UserExample
     * return int
     * */
    public static function editUserLevelInside($conn,$UserExample){
        $upUserSql="UPDATE  `users` SET `level` = `level` + 1 WHERE `token` = '$UserExample->token' and `level` = $UserExample->level";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 自动升级
     * entry UserExample
     * return int
     * */
    public static function editUserAutoLevelInside($conn,$level,$UserExample){
        $upUserSql="UPDATE  `users` SET `level` = $level WHERE `token` = '$UserExample->token' and `level` = $UserExample->level";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 待释放积分扣除
     * entry UserExample
     * return int
     * */
    public static function editUserCappingMinus($conn,$capping,$UserExample){
        $upUserSql="UPDATE  `users` SET `capping` = `capping` - $capping WHERE `token` = '$UserExample->token' and `capping` = $UserExample->capping";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 修改用户信息可添加字段
     * entry UserExample
     * return int
     * */
    public static function upUserInside($conn,$UserExample){
        $joinSql="";
        if($UserExample->truename !=null){
            $joinSql.=" `truename` = '$UserExample->truename',";
        }
        if($UserExample->password !=null){
            $UserExample->password = md5(md5($UserExample->password));
            $joinSql.=" `password` = '$UserExample->password',";
        }
        if($UserExample->pay_password !=null){
            $UserExample->pay_password = md5(md5($UserExample->pay_password));
            $joinSql.=" `pay_password` = '$UserExample->pay_password',";
        }
        if($UserExample->login_ip !=null){
            $joinSql.=" `login_ip` = '$UserExample->login_ip',";
        }
        if($UserExample->last_time !=null){
            $joinSql.=" `last_time` = $UserExample->last_time,";
        }
        if($UserExample->pid !=null){
            $joinSql.=" `pid` = $UserExample->pid,";
        }
        if($UserExample->head !=null){
            $joinSql.=" `head` = '$UserExample->head',";
        }
        if($UserExample->level_time !=null){
            $joinSql.=" `level_time` = $UserExample->level_time,";
        }
        if($UserExample->level !=null){
            $joinSql.=" `level` = $UserExample->level,";
        }
        if($UserExample->status !=null){
            $joinSql.=" `status` = $UserExample->status,";
        }
        if($UserExample->sach !=null){
            $joinSql.=" `sach` = $UserExample->sach,";
        }
        if($UserExample->is_get !=null){
            $joinSql.=" `is_get` = $UserExample->is_get,";
        }
        if($UserExample->share_rewards !=null){
            $joinSql.=" `share_rewards` = $UserExample->share_rewards,";
        }
        if($UserExample->svip !=null){
            $joinSql.=" `svip` = $UserExample->svip,";
        }
        if($UserExample->phone !=null){
            $joinSql.=" `phone` = '$UserExample->phone',";
        }
        if($UserExample->is_trade !=null){
            $joinSql.=" `is_trade` = '$UserExample->is_trade',";
        }
        if($UserExample->id_card !=null){
            $joinSql.=" `id_card` = '$UserExample->id_card',";
        }
        if($joinSql !=null){
            $innerJoin = rtrim($joinSql,",");
            $upUserSql="UPDATE  `users` SET $innerJoin WHERE `token` = '$UserExample->token'";
            return $conn->executeConn($upUserSql);
        }
    }
}